<?php
/**
*	此文件是流程模块【xmnbspymd.内部项目审批流程】对应控制器接口文件。
*/ 
class mode_xmnbspymdClassAction extends inputAction{
	
	/**
	*	重写函数：保存前处理，主要用于判断是否可以保存
	*	$table String 对应表名
	*	$arr Array 表单参数
	*	$id Int 对应表上记录Id 0添加时，大于0修改时
	*	$addbo Boolean 是否添加时
	*	return array('msg'=>'错误提示内容','rows'=> array()) 可返回空字符串，或者数组 rows 是可同时保存到数据库上数组
	*/
	protected function savebefore($table, $arr, $id, $addbo){
		$search_sdate = $arr["search_sdate"];
        $search_edate = $arr["search_edate"];
        $sql = "SELECT a.*, b.name as proxycompany FROM [Q]zbcg_new_info as a inner join [Q]company as b on a.comid = b.id where a.receiverdate >= '".$search_sdate." 00:00:01' and a.receiverdate <= '".$search_edate." 23:59:59'";
        $list = $this->db->getall($sql);
        //var_dump($search_sdate);var_dump($search_edate);exit();
        if(empty($list)){
            $this->backmsg('该时间段内没有内部项目');exit();
        }else{
            $new = "";
            $num = 0;
            $nus = 0;
            foreach ($list as $v){
                $new=$v['id'];
                $summaryInfo = m('zbcg_project_summary')->getone('`projectid` ='.$new, '`id`');
                $subInfo = m('zbcg_approve_sub')->getone('`projectid` ='.$new, '`id`');
                if(!empty($summaryInfo)){$num+=1;}
                if(!empty($subInfo)){$nus+=1;}
            }
            if($num==count($list) && $nus==count($list)){
                $this->backmsg('该时间段内没有需要审批的内部项目');exit();
            }

        }
	}
	
	/**
	*	重写函数：保存后处理，主要保存其他表数据
	*	$table String 对应表名
	*	$arr Array 表单参数
	*	$id Int 对应表上记录Id
	*	$addbo Boolean 是否添加时
	*/	
	protected function saveafter($table, $arr, $id, $addbo){
		$search_sdate = $arr["search_sdate"];
		$search_edate = $arr["search_edate"];
		$sql = "SELECT a.*, b.name as proxycompany FROM [Q]zbcg_new_info as a inner join [Q]company as b on a.comid = b.id where a.receiverdate >= '".$search_sdate." 00:00:01' and a.receiverdate <= '".$search_edate." 23:59:59'";
		$list = $this->db->getall($sql);
        for ($i = 0; $i < count($list); $i++) {
            $addArr = array(
                "mid"=>$id,
                "sort"=>0,
                "comid"=>$list[$i]["comid"],
                "name"=>$list[$i]["name"],
                "type"=>$list[$i]["type"],
                "budget"=>$list[$i]["budget"],
                "budget_item_name"=>$list[$i]["budget_item_name"],
                "funds_type"=>$list[$i]["funds_type"],
                "detail"=>$list[$i]["detail"],
                "other"=>$list[$i]["other"],
                "buymode"=>$list[$i]["buymode"],
                "projectadminer"=>"",
                "companyname"=>$list[$i]["proxycompany"],
                "producing_area"=>$list[$i]["producing_area"],
                "projectid" => $list[$i]["id"],
                "addtime"=>date("Y-m-d H:i:s"),
                "dept"=>$list[$i]["dept"],
                'upfile'=>$list[$i]['upfile'],
                'item_category' => $list[$i]['item_category'],
                'receiverdate' => $list[$i]['receiverdate'],
                'newid' => $list[$i]['mid']
            );
            //内部项目审批
            $subInfo = m('zbcg_approve_sub')->getone('`projectid`='.$list[$i]['id'], '`id`');
            if(empty($subInfo)){
                m('zbcg_approve_sub')->insert($addArr);
            }

            //招标情况汇总表
            $summaryInfo = m('zbcg_project_summary')->getone('`projectid`='.$list[$i]['id'], '`id`');
            if(empty($summaryInfo)) {
                $addArr['mid'] = $list[$i]['mid'];
                $addArr['projectid'] = $list[$i]["id"];
                $addArr['create_time'] = date("Y-m-d H:i:s");
                $addArr['update_time'] = date("Y-m-d H:i:s");
                unset($addArr['newid']);

                m('zbcg_project_summary')->insert($addArr);
            }
        }
	}
}	
			